//
//  YOProgressView.m
//  YOYOLive
//
//  Created by 王根强 on 16/9/19.
//  Copyright © 2016年 Mac. All rights reserved.
//

#import "YOProgressView.h"


@interface YOProgressView ()

/**
 *  进度条 progressView
 */
@property (nonatomic, strong) UIView *progressView;

/**
 *  progressView Rect
 */
@property (nonatomic) CGRect rect_progressView;

/**
 *  限制高度大小
 */
- (void)_setHeightRestrictionOfFrame:(CGFloat)height;


@property (nonatomic, assign) CGRect circlesSize;

@end

@implementation YOProgressView

#pragma mark -  initWithFrame
+ (instancetype)viewWithFrame:(CGRect)frame circlesSize:(CGRect)size WithStrokeColor:(UIColor *)strokeColor {
    return [[self alloc] initWithFrame:frame circlesSize:size WithStrokeColor:strokeColor];
}

- (instancetype)initWithFrame:(CGRect)frame circlesSize:(CGRect)size WithStrokeColor:(UIColor *)strokeColor {
    self = [super initWithFrame:frame];
    if (self) {
        self.circlesSize = size;
        [self initViewWithStrokeColor:strokeColor];
    }
    return self;
}

- (void)initViewWithStrokeColor:(UIColor *)strokeColor {
    [self addBackCircleWithSize:self.circlesSize.origin.x lineWidth:self.circlesSize.origin.y];
    [self addForeCircleWidthSize:self.circlesSize.size.width lineWidth:self.circlesSize.size.height WithStrokeColor:strokeColor];
}

//添加背景的圆环
- (void)addBackCircleWithSize:(CGFloat)radius lineWidth:(CGFloat)lineWidth {
    self.backCircle = [self createShapeLayerWithSize:radius lineWith:lineWidth color:[UIColor lightGrayColor]];
    self.backCircle.strokeStart = 0;
    self.backCircle.strokeEnd = 1;
    self.backCircle.strokeColor = [UIColor colorWithRed:80/255.0 green:80/255.0 blue:80/255.0 alpha:0.3].CGColor;
    [self.layer addSublayer:self.backCircle];
}

//前面的圆环
- (void)addForeCircleWidthSize:(CGFloat)radius lineWidth:(CGFloat)lineWidth WithStrokeColor:(UIColor *)strokeColor {
    self.foreCircle = [self createShapeLayerWithSize:radius lineWith:lineWidth color:[UIColor greenColor]];
    
    self.foreCircle.path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(radius, radius)
                                                          radius:radius-lineWidth/2
                                                      startAngle:-M_PI/2
                                                        endAngle:M_PI/180*270
                                                       clockwise:YES].CGPath;
    self.foreCircle.strokeStart = 0;
    self.foreCircle.strokeEnd = 0.8;
    self.foreCircle.strokeColor = strokeColor.CGColor;
    [self.layer addSublayer:self.foreCircle];
}

//创建圆环
- (CAShapeLayer *)createShapeLayerWithSize:(CGFloat)radius lineWith:(CGFloat)lineWidth color:(UIColor *)color {
    CGRect foreCircle_frame = CGRectMake(self.bounds.size.width/2-radius,
                                         self.bounds.size.height/2-radius,
                                         radius*2,
                                         radius*2);
    
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.frame = foreCircle_frame;
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(radius, radius)
                                                        radius:radius-lineWidth/2
                                                    startAngle:0
                                                      endAngle:M_PI*2
                                                     clockwise:YES];
    layer.path = path.CGPath;
    layer.fillColor = [UIColor clearColor].CGColor;
    layer.backgroundColor = [UIColor clearColor].CGColor;
    layer.strokeColor = color.CGColor;
    layer.lineWidth = lineWidth;
    layer.lineCap = @"round";
    return layer;
}

- (void)setProgressRoundValue:(CGFloat)progressRoundValue {
    if (self.foreCircle) {
        [UIView animateWithDuration:progressRoundValue animations:^{
            self.foreCircle.strokeEnd = progressRoundValue;
        }];
    }
}


#pragma mark - Privite Method
- (UIView *)progressView
{
    if (!_progressView) {
        _progressView = [[UIView alloc] initWithFrame:CGRectZero];
        _progressView.backgroundColor = [UIColor colorWithRed:0.973 green:0.745 blue:0.306 alpha:1.000];
        [self addSubview:self.progressView];
    }
    return _progressView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor colorWithRed:0.937 green:0.958 blue:1.000 alpha:1.000];
        
        [self _setHeightRestrictionOfFrame:frame.size.height];
    }
    return self;
}

- (void)_setHeightRestrictionOfFrame:(CGFloat)height
{
    CGRect rect = self.frame;
    
    _progressHeight = MIN(height, 100.0);
    _progressHeight = MAX(_progressHeight, 5.0);
    
    self.frame = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, _progressHeight);
    
    self.rect_progressView = CGRectZero;
    _rect_progressView.size.height = _progressHeight;
    self.progressView.frame = self.rect_progressView;
    
    self.layer.cornerRadius = self.progressView.layer.cornerRadius =  _progressHeight / 2.0;
}

#pragma mark - Setter

- (void)setProgressHeight:(CGFloat)progressHeight
{
    [self _setHeightRestrictionOfFrame:progressHeight];
}

- (void)setProgressTintColor:(UIColor *)progressTintColor
{
    _progressTintColor = progressTintColor;
    
    self.backgroundColor = _progressTintColor;
}

- (void)setTrackTintColor:(UIColor *)trackTintColor
{
    _trackTintColor = trackTintColor;
    
    self.progressView.backgroundColor = _trackTintColor;
}

- (void)setProgressValue:(CGFloat)progressValue
{
    _progressValue = progressValue;
    
    _progressValue = MIN(progressValue, self.bounds.size.width);
    
    _rect_progressView.size.width = _progressValue;
    
    
    CGFloat maxValue = self.bounds.size.width;
    
    double durationValue = (_progressValue/2.0) / maxValue + .5;
    
    [UIView animateWithDuration:durationValue animations:^{
        
        self.progressView.frame = _rect_progressView;
    }];
}


@end
